
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>04-Koa2 · 千锋大前端小册-GP19-Node.js</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="古艺散人">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchors/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-anchor-navigation-ex/style/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    

        
    
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="05-MongoDB.html" />
    
    
    <link rel="prev" href="03-Express.html" />
    

    
        <link rel="shortcut icon" href='../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="bookmark" href='../source/images/favicon.jpg' type="image/x-icon">
    
    
        <link rel="apple-touch-icon" href='../source/images/apple-touch-icon.jpg'>
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="../source/images/apple-touch-icon.jpg">
        
        <link rel="apple-touch-icon" sizes="180x180" href="../source/images/apple-touch-icon.jpg">
        
    

    <style>
    @media only screen and (max-width: 640px) {
        .book-header .hidden-mobile {
            display: none;
        }
    }
    </style>
    <script>
        window["gitbook-plugin-github-buttons"] = {"buttons":[{"user":"lurongtao","repo":"felixbooks-kbone","type":"star","size":"small","count":true}]};
    </script>

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="01-Node.js基础.html">
            
                <a href="01-Node.js基础.html">
            
                    
                    基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="01-Node.js基础.html">
            
                <a href="01-Node.js基础.html">
            
                    
                    01-Node.js基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="02-yarn.html">
            
                <a href="02-yarn.html">
            
                    
                    02-Yarn入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="03-Express.html">
            
                <a href="03-Express.html">
            
                    
                    03-Express
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.4" data-path="04-Koa2.html">
            
                <a href="04-Koa2.html">
            
                    
                    04-Koa2
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="05-MongoDB.html">
            
                <a href="05-MongoDB.html">
            
                    
                    05-MongoDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="06-socket.html">
            
                <a href="06-socket.html">
            
                    
                    06-socket编程
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../advanced/01-Node中间件.html">
            
                <a href="../advanced/01-Node中间件.html">
            
                    
                    高级
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../advanced/01-Node中间件.html">
            
                <a href="../advanced/01-Node中间件.html">
            
                    
                    01-Node中间件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../advanced/02-Node事件循环.html">
            
                <a href="../advanced/02-Node事件循环.html">
            
                    
                    02-Node事件循环
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../advanced/03-Node中间层.html">
            
                <a href="../advanced/03-Node中间层.html">
            
                    
                    03-Node中间层
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../thanks/">
            
                <a href="../thanks/">
            
                    
                    感谢
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >04-Koa2</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <div id="anchor-navigation-ex-navbar"><i class="fa fa-navicon"></i><ul><li><span class="title-icon "></span><a href="#&#x2170;&#x3001;koa2-&#x5F00;&#x59CB;"><b></b>&#x2160;&#x3001;Koa2 &#x5F00;&#x59CB;</a></li><ul><ul><li><span class="title-icon "></span><a href="#&#x4E00;&#x3001;koa2-&#x5FEB;&#x901F;&#x5F00;&#x59CB;"><b></b>&#x4E00;&#x3001;koa2 &#x5FEB;&#x901F;&#x5F00;&#x59CB;</a></li><li><span class="title-icon "></span><a href="#&#x4E8C;&#x3001;asyncawait&#x4F7F;&#x7528;"><b></b>&#x4E8C;&#x3001;async/await&#x4F7F;&#x7528;</a></li><li><span class="title-icon "></span><a href="#&#x4E09;&#x3001;koa2&#x7B80;&#x6790;&#x7ED3;&#x6784;"><b></b>&#x4E09;&#x3001;koa2&#x7B80;&#x6790;&#x7ED3;&#x6784;</a></li><li><span class="title-icon "></span><a href="#&#x56DB;&#x3001;koa&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;&#x548C;&#x4F7F;&#x7528;"><b></b>&#x56DB;&#x3001;koa&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;&#x548C;&#x4F7F;&#x7528;</a></li></ul></ul><li><span class="title-icon "></span><a href="#&#x2171;&#x3001;&#x8DEF;&#x7531;"><b></b>&#x2161;&#x3001;&#x8DEF;&#x7531;</a></li><ul><ul><li><span class="title-icon "></span><a href="#&#x4E00;&#x3001;koa2-&#x539F;&#x751F;&#x8DEF;&#x7531;&#x5B9E;&#x73B0;"><b></b>&#x4E00;&#x3001;koa2 &#x539F;&#x751F;&#x8DEF;&#x7531;&#x5B9E;&#x73B0;</a></li><li><span class="title-icon "></span><a href="#&#x4E8C;&#x3001;koa-router&#x4E2D;&#x95F4;&#x4EF6;"><b></b>&#x4E8C;&#x3001;koa-router&#x4E2D;&#x95F4;&#x4EF6;</a></li></ul></ul><li><span class="title-icon "></span><a href="#&#x2172;&#x3001;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;"><b></b>&#x2162;&#x3001;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;</a></li><ul><ul><li><span class="title-icon "></span><a href="#&#x4E00;&#x3001;get&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;"><b></b>&#x4E00;&#x3001;GET&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;</a></li><li><span class="title-icon "></span><a href="#&#x4E8C;&#x3001;post&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x83B7;&#x53D6;"><b></b>&#x4E8C;&#x3001;POST&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x83B7;&#x53D6;</a></li><li><span class="title-icon "></span><a href="#&#x4E09;&#x3001;koa-bodyparser&#x4E2D;&#x95F4;&#x4EF6;"><b></b>&#x4E09;&#x3001;koa-bodyparser&#x4E2D;&#x95F4;&#x4EF6;</a></li></ul></ul><li><span class="title-icon "></span><a href="#&#x2173;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x52A0;&#x8F7D;"><b></b>&#x2163;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x52A0;&#x8F7D;</a></li><ul><ul><li><span class="title-icon "></span><a href="#koa-static&#x4E2D;&#x95F4;&#x4EF6;&#x4F7F;&#x7528;"><b></b>koa-static&#x4E2D;&#x95F4;&#x4EF6;&#x4F7F;&#x7528;</a></li></ul></ul><li><span class="title-icon "></span><a href="#&#x2174;&#x3001;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><b></b>&#x2164;&#x3001;&#x6A21;&#x677F;&#x5F15;&#x64CE;</a></li><ul><ul><li><span class="title-icon "></span><a href="#koa2&#x52A0;&#x8F7D;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><b></b>koa2&#x52A0;&#x8F7D;&#x6A21;&#x677F;&#x5F15;&#x64CE;</a></li></ul></ul></ul></div><a href="#&#x2170;&#x3001;koa2-&#x5F00;&#x59CB;" id="anchorNavigationExGoTop"><i class="fa fa-arrow-up"></i></a><h1 id="&#x2170;&#x3001;koa2-&#x5F00;&#x59CB;"><a name="&#x2170;&#x3001;koa2-&#x5F00;&#x59CB;" class="anchor-navigation-ex-anchor" href="#&#x2170;&#x3001;koa2-&#x5F00;&#x59CB;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x2170;&#x3001;koa2-&#x5F00;&#x59CB;" class="plugin-anchor" href="#&#x2170;&#x3001;koa2-&#x5F00;&#x59CB;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x2160;&#x3001;Koa2 &#x5F00;&#x59CB;</h1>
<h3 id="&#x4E00;&#x3001;koa2-&#x5FEB;&#x901F;&#x5F00;&#x59CB;"><a name="&#x4E00;&#x3001;koa2-&#x5FEB;&#x901F;&#x5F00;&#x59CB;" class="anchor-navigation-ex-anchor" href="#&#x4E00;&#x3001;koa2-&#x5FEB;&#x901F;&#x5F00;&#x59CB;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E00;&#x3001;koa2-&#x5FEB;&#x901F;&#x5F00;&#x59CB;" class="plugin-anchor" href="#&#x4E00;&#x3001;koa2-&#x5FEB;&#x901F;&#x5F00;&#x59CB;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E00;&#x3001;koa2 &#x5FEB;&#x901F;&#x5F00;&#x59CB;</h3>
<h4 id="1&#x3001;&#x73AF;&#x5883;&#x51C6;&#x5907;"><a name="1&#x3001;&#x73AF;&#x5883;&#x51C6;&#x5907;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x73AF;&#x5883;&#x51C6;&#x5907;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x73AF;&#x5883;&#x51C6;&#x5907;" class="plugin-anchor" href="#1&#x3001;&#x73AF;&#x5883;&#x51C6;&#x5907;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x73AF;&#x5883;&#x51C6;&#x5907;</h4>
<p>&#x56E0;&#x4E3A;node.js v7.6.0&#x5F00;&#x59CB;&#x5B8C;&#x5168;&#x652F;&#x6301;async/await&#xFF0C;&#x4E0D;&#x9700;&#x8981;&#x52A0;flag&#xFF0C;&#x6240;&#x4EE5;node.js&#x73AF;&#x5883;&#x90FD;&#x8981;7.6.0&#x4EE5;&#x4E0A;
node.js&#x73AF;&#x5883; &#x7248;&#x672C;v7.6&#x4EE5;&#x4E0A;
npm &#x7248;&#x672C;3.x&#x4EE5;&#x4E0A;</p>
<h4 id="2&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;"><a name="2&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;" class="plugin-anchor" href="#2&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;</h4>
<h5 id="21-&#x5B89;&#x88C5;koa2"><a name="21-&#x5B89;&#x88C5;koa2" class="anchor-navigation-ex-anchor" href="#21-&#x5B89;&#x88C5;koa2"><i class="fa fa-link" aria-hidden="true"></i></a><a name="21-&#x5B89;&#x88C5;koa2" class="plugin-anchor" href="#21-&#x5B89;&#x88C5;koa2"><i class="fa fa-link" aria-hidden="true"></i></a>2.1 &#x5B89;&#x88C5;koa2</h5>
<pre><code class="lang-s"># &#x521D;&#x59CB;&#x5316;package.json
npm init

# &#x5B89;&#x88C5;koa2 
npm install koa
</code></pre>
<h5 id="22-hello-world-&#x4EE3;&#x7801;"><a name="22-hello-world-&#x4EE3;&#x7801;" class="anchor-navigation-ex-anchor" href="#22-hello-world-&#x4EE3;&#x7801;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="22-hello-world-&#x4EE3;&#x7801;" class="plugin-anchor" href="#22-hello-world-&#x4EE3;&#x7801;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2 hello world &#x4EE3;&#x7801;</h5>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>)
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()

app.use( <span class="hljs-keyword">async</span> ( ctx ) =&gt; {
  ctx.body = <span class="hljs-string">&apos;hello koa2&apos;</span>
})

app.listen(<span class="hljs-number">3000</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;[demo] start-quick is starting at port 3000&apos;</span>)
</code></pre>
<h5 id="23-&#x542F;&#x52A8;demo"><a name="23-&#x542F;&#x52A8;demo" class="anchor-navigation-ex-anchor" href="#23-&#x542F;&#x52A8;demo"><i class="fa fa-link" aria-hidden="true"></i></a><a name="23-&#x542F;&#x52A8;demo" class="plugin-anchor" href="#23-&#x542F;&#x52A8;demo"><i class="fa fa-link" aria-hidden="true"></i></a>2.3 &#x542F;&#x52A8;demo</h5>
<p>&#x7531;&#x4E8E;koa2&#x662F;&#x57FA;&#x4E8E;async/await&#x64CD;&#x4F5C;&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x76EE;&#x524D;node.js 7.x&#x7684;harmony&#x6A21;&#x5F0F;&#x4E0B;&#x624D;&#x80FD;&#x4F7F;&#x7528;&#xFF0C;&#x6240;&#x4EE5;&#x542F;&#x52A8;&#x7684;&#x65F6;&#x7684;&#x811A;&#x672C;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-s">node index.js
</code></pre>
<h3 id="&#x4E8C;&#x3001;asyncawait&#x4F7F;&#x7528;"><a name="&#x4E8C;&#x3001;asyncawait&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#&#x4E8C;&#x3001;asyncawait&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E8C;&#x3001;asyncawait&#x4F7F;&#x7528;" class="plugin-anchor" href="#&#x4E8C;&#x3001;asyncawait&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C;&#x3001;async/await&#x4F7F;&#x7528;</h3>
<h4 id="1&#x3001;&#x5FEB;&#x901F;&#x4E0A;&#x624B;&#x7406;&#x89E3;"><a name="1&#x3001;&#x5FEB;&#x901F;&#x4E0A;&#x624B;&#x7406;&#x89E3;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x5FEB;&#x901F;&#x4E0A;&#x624B;&#x7406;&#x89E3;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x5FEB;&#x901F;&#x4E0A;&#x624B;&#x7406;&#x89E3;" class="plugin-anchor" href="#1&#x3001;&#x5FEB;&#x901F;&#x4E0A;&#x624B;&#x7406;&#x89E3;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x5FEB;&#x901F;&#x4E0A;&#x624B;&#x7406;&#x89E3;</h4>
<p>&#x5148;&#x590D;&#x5236;&#x4EE5;&#x4E0B;&#x8FD9;&#x6BB5;&#x4EE3;&#x7801;&#xFF0C;&#x5728;&#x7C98;&#x8D34;&#x5728;chrome&#x7684;&#x63A7;&#x5236;&#x53F0;console&#x4E2D;&#xFF0C;&#x6309;&#x56DE;&#x8F66;&#x952E;&#x6267;&#x884C;</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getSyncTime</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">let</span> startTime = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getTime()
      setTimeout(() =&gt; {
        <span class="hljs-keyword">let</span> endTime = <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().getTime()
        <span class="hljs-keyword">let</span> data = endTime - startTime
        resolve( data )
      }, <span class="hljs-number">500</span>)
    } <span class="hljs-keyword">catch</span> ( err ) {
      reject( err )
    }
  })
}

<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getSyncData</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> time = <span class="hljs-keyword">await</span> getSyncTime()
  <span class="hljs-keyword">let</span> data = <span class="hljs-string">`endTime - startTime = <span class="hljs-subst">${time}</span>`</span>
  <span class="hljs-keyword">return</span> data
}

<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getData</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> data = <span class="hljs-keyword">await</span> getSyncData()
  <span class="hljs-built_in">console</span>.log( data )
}

getData()
</code></pre>
<h4 id="2&#x3001;&#x4ECE;&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x53EF;&#x4EE5;&#x770B;&#x51FA;-asyncawait-&#x7684;&#x7279;&#x70B9;&#xFF1A;"><a name="2&#x3001;&#x4ECE;&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x53EF;&#x4EE5;&#x770B;&#x51FA;-asyncawait-&#x7684;&#x7279;&#x70B9;&#xFF1A;" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x4ECE;&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x53EF;&#x4EE5;&#x770B;&#x51FA;-asyncawait-&#x7684;&#x7279;&#x70B9;&#xFF1A;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x4ECE;&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x53EF;&#x4EE5;&#x770B;&#x51FA;-asyncawait-&#x7684;&#x7279;&#x70B9;&#xFF1A;" class="plugin-anchor" href="#2&#x3001;&#x4ECE;&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x53EF;&#x4EE5;&#x770B;&#x51FA;-asyncawait-&#x7684;&#x7279;&#x70B9;&#xFF1A;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x4ECE;&#x4E0A;&#x8FF0;&#x4F8B;&#x5B50;&#x53EF;&#x4EE5;&#x770B;&#x51FA; async/await &#x7684;&#x7279;&#x70B9;&#xFF1A;</h4>
<ul>
<li>&#x53EF;&#x4EE5;&#x8BA9;&#x5F02;&#x6B65;&#x903B;&#x8F91;&#x7528;&#x540C;&#x6B65;&#x5199;&#x6CD5;&#x5B9E;&#x73B0;</li>
<li>&#x6700;&#x5E95;&#x5C42;&#x7684;await&#x8FD4;&#x56DE;&#x9700;&#x8981;&#x662F;Promise&#x5BF9;&#x8C61;</li>
<li>&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x591A;&#x5C42; async function &#x7684;&#x540C;&#x6B65;&#x5199;&#x6CD5;&#x4EE3;&#x66FF;&#x4F20;&#x7EDF;&#x7684;callback&#x5D4C;&#x5957;</li>
</ul>
<h3 id="&#x4E09;&#x3001;koa2&#x7B80;&#x6790;&#x7ED3;&#x6784;"><a name="&#x4E09;&#x3001;koa2&#x7B80;&#x6790;&#x7ED3;&#x6784;" class="anchor-navigation-ex-anchor" href="#&#x4E09;&#x3001;koa2&#x7B80;&#x6790;&#x7ED3;&#x6784;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E09;&#x3001;koa2&#x7B80;&#x6790;&#x7ED3;&#x6784;" class="plugin-anchor" href="#&#x4E09;&#x3001;koa2&#x7B80;&#x6790;&#x7ED3;&#x6784;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E09;&#x3001;koa2&#x7B80;&#x6790;&#x7ED3;&#x6784;</h3>
<h4 id="1&#x3001;&#x6E90;&#x7801;&#x6587;&#x4EF6;"><a name="1&#x3001;&#x6E90;&#x7801;&#x6587;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x6E90;&#x7801;&#x6587;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x6E90;&#x7801;&#x6587;&#x4EF6;" class="plugin-anchor" href="#1&#x3001;&#x6E90;&#x7801;&#x6587;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x6E90;&#x7801;&#x6587;&#x4EF6;</h4>
<p>&#x251C;&#x2500;&#x2500; lib
&#x2502;   &#x251C;&#x2500;&#x2500; application.js
&#x2502;   &#x251C;&#x2500;&#x2500; context.js
&#x2502;   &#x251C;&#x2500;&#x2500; request.js
&#x2502;   &#x2514;&#x2500;&#x2500; response.js
&#x2514;&#x2500;&#x2500; package.json</p>
<p>&#x8FD9;&#x4E2A;&#x5C31;&#x662F; GitHub <a href="https://github.com/koajs/koa&#x4E0A;&#x5F00;&#x6E90;&#x7684;koa2&#x6E90;&#x7801;&#x7684;&#x6E90;&#x6587;&#x4EF6;&#x7ED3;&#x6784;&#xFF0C;&#x6838;&#x5FC3;&#x4EE3;&#x7801;&#x5C31;&#x662F;lib&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x56DB;&#x4E2A;&#x6587;&#x4EF6;" target="_blank">https://github.com/koajs/koa&#x4E0A;&#x5F00;&#x6E90;&#x7684;koa2&#x6E90;&#x7801;&#x7684;&#x6E90;&#x6587;&#x4EF6;&#x7ED3;&#x6784;&#xFF0C;&#x6838;&#x5FC3;&#x4EE3;&#x7801;&#x5C31;&#x662F;lib&#x76EE;&#x5F55;&#x4E0B;&#x7684;&#x56DB;&#x4E2A;&#x6587;&#x4EF6;</a></p>
<ul>
<li>application.js &#x662F;&#x6574;&#x4E2A;koa2 &#x7684;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#xFF0C;&#x5C01;&#x88C5;&#x4E86;context&#xFF0C;request&#xFF0C;response&#xFF0C;&#x4EE5;&#x53CA;&#x6700;&#x6838;&#x5FC3;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x5904;&#x7406;&#x6D41;&#x7A0B;&#x3002;</li>
<li>context.js &#x5904;&#x7406;&#x5E94;&#x7528;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x91CC;&#x9762;&#x76F4;&#x63A5;&#x5C01;&#x88C5;&#x90E8;&#x5206;request.js&#x548C;response.js&#x7684;&#x65B9;&#x6CD5;</li>
<li>request.js &#x5904;&#x7406;http&#x8BF7;&#x6C42;</li>
<li>response.js &#x5904;&#x7406;http&#x54CD;&#x5E94;</li>
</ul>
<h4 id="2&#x3001;koa2&#x7279;&#x6027;"><a name="2&#x3001;koa2&#x7279;&#x6027;" class="anchor-navigation-ex-anchor" href="#2&#x3001;koa2&#x7279;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;koa2&#x7279;&#x6027;" class="plugin-anchor" href="#2&#x3001;koa2&#x7279;&#x6027;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;koa2&#x7279;&#x6027;</h4>
<ul>
<li>&#x53EA;&#x63D0;&#x4F9B;&#x5C01;&#x88C5;&#x597D;http&#x4E0A;&#x4E0B;&#x6587;&#x3001;&#x8BF7;&#x6C42;&#x3001;&#x54CD;&#x5E94;&#xFF0C;&#x4EE5;&#x53CA;&#x57FA;&#x4E8E;async/await&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x5BB9;&#x5668;&#x3002;</li>
<li>&#x5229;&#x7528;ES7&#x7684;async/await&#x7684;&#x6765;&#x5904;&#x7406;&#x4F20;&#x7EDF;&#x56DE;&#x8C03;&#x5D4C;&#x5957;&#x95EE;&#x9898;&#x548C;&#x4EE3;&#x66FF;koa@1&#x7684;generator&#xFF0C;&#x4F46;&#x662F;&#x9700;&#x8981;&#x5728;node.js 7.x&#x7684;harmony&#x6A21;&#x5F0F;&#x4E0B;&#x624D;&#x80FD;&#x652F;&#x6301;async/await&#x3002;</li>
<li>&#x4E2D;&#x95F4;&#x4EF6;&#x53EA;&#x652F;&#x6301; async/await &#x5C01;&#x88C5;&#x7684;&#xFF0C;&#x5982;&#x679C;&#x8981;&#x4F7F;&#x7528;koa@1&#x57FA;&#x4E8E;generator&#x4E2D;&#x95F4;&#x4EF6;&#xFF0C;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x4E2D;&#x95F4;&#x4EF6;koa-convert&#x5C01;&#x88C5;&#x4E00;&#x4E0B;&#x624D;&#x80FD;&#x4F7F;&#x7528;&#x3002;</li>
</ul>
<h3 id="&#x56DB;&#x3001;koa&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;&#x548C;&#x4F7F;&#x7528;"><a name="&#x56DB;&#x3001;koa&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;&#x548C;&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#&#x56DB;&#x3001;koa&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;&#x548C;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x56DB;&#x3001;koa&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;&#x548C;&#x4F7F;&#x7528;" class="plugin-anchor" href="#&#x56DB;&#x3001;koa&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;&#x548C;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x56DB;&#x3001;koa&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;&#x548C;&#x4F7F;&#x7528;</h3>
<ul>
<li>koa v1&#x548C;v2&#x4E2D;&#x4F7F;&#x7528;&#x5230;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x5F00;&#x53D1;&#x548C;&#x4F7F;&#x7528;</li>
<li>generator &#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;&#x5728;koa v1&#x548C;v2&#x4E2D;&#x4F7F;&#x7528;</li>
<li>async await &#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;&#x548C;&#x53EA;&#x80FD;&#x5728;koa v2&#x4E2D;&#x4F7F;&#x7528;</li>
</ul>
<h4 id="1&#x3001;generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><a name="1&#x3001;generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;" class="anchor-navigation-ex-anchor" href="#1&#x3001;generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;" class="plugin-anchor" href="#1&#x3001;generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;</h4>
<h5 id="11-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><a name="11-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;" class="anchor-navigation-ex-anchor" href="#11-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="11-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;" class="plugin-anchor" href="#11-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1 generator&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;</h5>
<blockquote>
<p>generator&#x4E2D;&#x95F4;&#x4EF6;&#x8FD4;&#x56DE;&#x7684;&#x5E94;&#x8BE5;&#x662F;function * () &#x51FD;&#x6570;</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-comment">/* ./middleware/logger-generator.js */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">log</span>(<span class="hljs-params"> ctx </span>) </span>{
  <span class="hljs-built_in">console</span>.log( ctx.method, ctx.header.host + ctx.url )
}

<span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> * (<span class="hljs-params"> next </span>) </span>{

    <span class="hljs-comment">// &#x6267;&#x884C;&#x4E2D;&#x95F4;&#x4EF6;&#x7684;&#x64CD;&#x4F5C;</span>
    log( <span class="hljs-keyword">this</span> )

    <span class="hljs-keyword">if</span> ( next ) {
      <span class="hljs-keyword">yield</span> next
    }
  }
}
</code></pre>
<h5 id="12-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa1&#x4E2D;&#x7684;&#x4F7F;&#x7528;"><a name="12-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa1&#x4E2D;&#x7684;&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#12-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa1&#x4E2D;&#x7684;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="12-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa1&#x4E2D;&#x7684;&#x4F7F;&#x7528;" class="plugin-anchor" href="#12-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa1&#x4E2D;&#x7684;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2 generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa@1&#x4E2D;&#x7684;&#x4F7F;&#x7528;</h5>
<blockquote>
<p>generator &#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa v1&#x4E2D;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;use&#x4F7F;&#x7528;</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>)  <span class="hljs-comment">// koa v1</span>
<span class="hljs-keyword">const</span> loggerGenerator  = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./middleware/logger-generator&apos;</span>)
<span class="hljs-keyword">const</span> app = koa()

app.use(loggerGenerator())

app.use(<span class="hljs-function"><span class="hljs-keyword">function</span> *(<span class="hljs-params"> </span>) </span>{
    <span class="hljs-keyword">this</span>.body = <span class="hljs-string">&apos;hello world!&apos;</span>
})

app.listen(<span class="hljs-number">3000</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;the server is starting at port 3000&apos;</span>)
</code></pre>
<h5 id="13-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa2&#x4E2D;&#x7684;&#x4F7F;&#x7528;"><a name="13-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa2&#x4E2D;&#x7684;&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#13-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa2&#x4E2D;&#x7684;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="13-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa2&#x4E2D;&#x7684;&#x4F7F;&#x7528;" class="plugin-anchor" href="#13-generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa2&#x4E2D;&#x7684;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>1.3 generator&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa@2&#x4E2D;&#x7684;&#x4F7F;&#x7528;</h5>
<blockquote>
<p>generator &#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa v2&#x4E2D;&#x9700;&#x8981;&#x7528;koa-convert&#x5C01;&#x88C5;&#x4E00;&#x4E0B;&#x624D;&#x80FD;&#x4F7F;&#x7528;</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>) <span class="hljs-comment">// koa v2</span>
<span class="hljs-keyword">const</span> convert = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa-convert&apos;</span>)
<span class="hljs-keyword">const</span> loggerGenerator  = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./middleware/logger-generator&apos;</span>)
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()

app.use(convert(loggerGenerator()))

app.use(( ctx ) =&gt; {
    ctx.body = <span class="hljs-string">&apos;hello world!&apos;</span>
})

app.listen(<span class="hljs-number">3000</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;the server is starting at port 3000&apos;</span>)
</code></pre>
<h4 id="2&#x3001;async&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><a name="2&#x3001;async&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;" class="anchor-navigation-ex-anchor" href="#2&#x3001;async&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;async&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;" class="plugin-anchor" href="#2&#x3001;async&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;async&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;</h4>
<h5 id="21-async-&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><a name="21-async-&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;" class="anchor-navigation-ex-anchor" href="#21-async-&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="21-async-&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;" class="plugin-anchor" href="#21-async-&#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1 async &#x4E2D;&#x95F4;&#x4EF6;&#x5F00;&#x53D1;</h5>
<pre><code class="lang-js"><span class="hljs-comment">/* ./middleware/logger-async.js */</span>

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">log</span>(<span class="hljs-params"> ctx </span>) </span>{
    <span class="hljs-built_in">console</span>.log( ctx.method, ctx.header.host + ctx.url )
}

<span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"> ctx, next </span>) </span>{
    log(ctx);
    <span class="hljs-keyword">await</span> next()
  }
}
</code></pre>
<h5 id="22-async-&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa2&#x4E2D;&#x4F7F;&#x7528;"><a name="22-async-&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa2&#x4E2D;&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#22-async-&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa2&#x4E2D;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="22-async-&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa2&#x4E2D;&#x4F7F;&#x7528;" class="plugin-anchor" href="#22-async-&#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa2&#x4E2D;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2 async &#x4E2D;&#x95F4;&#x4EF6;&#x5728;koa@2&#x4E2D;&#x4F7F;&#x7528;</h5>
<blockquote>
<p>async &#x4E2D;&#x95F4;&#x4EF6;&#x53EA;&#x80FD;&#x5728; koa v2&#x4E2D;&#x4F7F;&#x7528;</p>
</blockquote>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>) <span class="hljs-comment">// koa v2</span>
<span class="hljs-keyword">const</span> loggerAsync  = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./middleware/logger-async&apos;</span>)
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()

app.use(loggerAsync())

app.use(( ctx ) =&gt; {
    ctx.body = <span class="hljs-string">&apos;hello world!&apos;</span>
})

app.listen(<span class="hljs-number">3000</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;the server is starting at port 3000&apos;</span>)
</code></pre>
<h1 id="&#x2171;&#x3001;&#x8DEF;&#x7531;"><a name="&#x2171;&#x3001;&#x8DEF;&#x7531;" class="anchor-navigation-ex-anchor" href="#&#x2171;&#x3001;&#x8DEF;&#x7531;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x2171;&#x3001;&#x8DEF;&#x7531;" class="plugin-anchor" href="#&#x2171;&#x3001;&#x8DEF;&#x7531;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x2161;&#x3001;&#x8DEF;&#x7531;</h1>
<h3 id="&#x4E00;&#x3001;koa2-&#x539F;&#x751F;&#x8DEF;&#x7531;&#x5B9E;&#x73B0;"><a name="&#x4E00;&#x3001;koa2-&#x539F;&#x751F;&#x8DEF;&#x7531;&#x5B9E;&#x73B0;" class="anchor-navigation-ex-anchor" href="#&#x4E00;&#x3001;koa2-&#x539F;&#x751F;&#x8DEF;&#x7531;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E00;&#x3001;koa2-&#x539F;&#x751F;&#x8DEF;&#x7531;&#x5B9E;&#x73B0;" class="plugin-anchor" href="#&#x4E00;&#x3001;koa2-&#x539F;&#x751F;&#x8DEF;&#x7531;&#x5B9E;&#x73B0;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E00;&#x3001;koa2 &#x539F;&#x751F;&#x8DEF;&#x7531;&#x5B9E;&#x73B0;</h3>
<h4 id="1&#x3001;&#x7B80;&#x5355;&#x4F8B;&#x5B50;"><a name="1&#x3001;&#x7B80;&#x5355;&#x4F8B;&#x5B50;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x7B80;&#x5355;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x7B80;&#x5355;&#x4F8B;&#x5B50;" class="plugin-anchor" href="#1&#x3001;&#x7B80;&#x5355;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x7B80;&#x5355;&#x4F8B;&#x5B50;</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>)
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()

app.use( <span class="hljs-keyword">async</span> ( ctx ) =&gt; {
  <span class="hljs-keyword">let</span> url = ctx.request.url
  ctx.body = url
})
app.listen(<span class="hljs-number">3000</span>)
</code></pre>
<p>&#x8BBF;&#x95EE; <a href="http://localhost:3000/hello/world" target="_blank">http://localhost:3000/hello/world</a> &#x9875;&#x9762;&#x4F1A;&#x8F93;&#x51FA; /hello/world&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x4E0A;&#x4E0B;&#x6587;&#x7684;&#x8BF7;&#x6C42;request&#x5BF9;&#x8C61;&#x4E2D;url&#x4E4B;&#x5C31;&#x662F;&#x5F53;&#x524D;&#x8BBF;&#x95EE;&#x7684;&#x8DEF;&#x5F84;&#x540D;&#x79F0;&#xFF0C;&#x53EF;&#x4EE5;&#x6839;&#x636E;ctx.request.url &#x901A;&#x8FC7;&#x4E00;&#x5B9A;&#x7684;&#x5224;&#x65AD;&#x6216;&#x8005;&#x6B63;&#x5219;&#x5339;&#x914D;&#x5C31;&#x53EF;&#x4EE5;&#x5B9A;&#x5236;&#x51FA;&#x6240;&#x9700;&#x8981;&#x7684;&#x8DEF;&#x7531;&#x3002;</p>
<h4 id="2&#x3001;&#x5B9A;&#x5236;&#x5316;&#x7684;&#x8DEF;&#x7531;"><a name="2&#x3001;&#x5B9A;&#x5236;&#x5316;&#x7684;&#x8DEF;&#x7531;" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x5B9A;&#x5236;&#x5316;&#x7684;&#x8DEF;&#x7531;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x5B9A;&#x5236;&#x5316;&#x7684;&#x8DEF;&#x7531;" class="plugin-anchor" href="#2&#x3001;&#x5B9A;&#x5236;&#x5316;&#x7684;&#x8DEF;&#x7531;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x5B9A;&#x5236;&#x5316;&#x7684;&#x8DEF;&#x7531;</h4>
<p>demo&#x6E90;&#x7801;</p>
<p><a href="https://github.com/ChenShenhai/koa2-note/tree/master/demo/route-simple" target="_blank">https://github.com/ChenShenhai/koa2-note/tree/master/demo/route-simple</a></p>
<h5 id="21-&#x6E90;&#x7801;&#x6587;&#x4EF6;&#x76EE;&#x5F55;"><a name="21-&#x6E90;&#x7801;&#x6587;&#x4EF6;&#x76EE;&#x5F55;" class="anchor-navigation-ex-anchor" href="#21-&#x6E90;&#x7801;&#x6587;&#x4EF6;&#x76EE;&#x5F55;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="21-&#x6E90;&#x7801;&#x6587;&#x4EF6;&#x76EE;&#x5F55;" class="plugin-anchor" href="#21-&#x6E90;&#x7801;&#x6587;&#x4EF6;&#x76EE;&#x5F55;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1 &#x6E90;&#x7801;&#x6587;&#x4EF6;&#x76EE;&#x5F55;</h5>
<pre><code>.
&#x251C;&#x2500;&#x2500; index.js
&#x251C;&#x2500;&#x2500; package.json
&#x2514;&#x2500;&#x2500; view
    &#x251C;&#x2500;&#x2500; 404.html
    &#x251C;&#x2500;&#x2500; index.html
    &#x2514;&#x2500;&#x2500; todo.html
</code></pre><h5 id="22-demo&#x6E90;&#x7801;"><a name="22-demo&#x6E90;&#x7801;" class="anchor-navigation-ex-anchor" href="#22-demo&#x6E90;&#x7801;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="22-demo&#x6E90;&#x7801;" class="plugin-anchor" href="#22-demo&#x6E90;&#x7801;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2 demo&#x6E90;&#x7801;</h5>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>)
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>)
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()

<span class="hljs-comment">/**
 * &#x7528;Promise&#x5C01;&#x88C5;&#x5F02;&#x6B65;&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x65B9;&#x6CD5;
 * @param  {string} page html&#x6587;&#x4EF6;&#x540D;&#x79F0;
 * @return {promise}      
 */</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">render</span>(<span class="hljs-params"> page </span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(( resolve, reject ) =&gt; {
    <span class="hljs-keyword">let</span> viewUrl = <span class="hljs-string">`./view/<span class="hljs-subst">${page}</span>`</span>
    fs.readFile(viewUrl, <span class="hljs-string">&quot;binary&quot;</span>, ( err, data ) =&gt; {
      <span class="hljs-keyword">if</span> ( err ) {
        reject( err )
      } <span class="hljs-keyword">else</span> {
        resolve( data )
      }
    })
  })
}

<span class="hljs-comment">/**
 * &#x6839;&#x636E;URL&#x83B7;&#x53D6;HTML&#x5185;&#x5BB9;
 * @param  {string} url koa2&#x4E0A;&#x4E0B;&#x6587;&#x7684;url&#xFF0C;ctx.url
 * @return {string}     &#x83B7;&#x53D6;HTML&#x6587;&#x4EF6;&#x5185;&#x5BB9;
 */</span>
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">route</span>(<span class="hljs-params"> url </span>) </span>{
  <span class="hljs-keyword">let</span> view = <span class="hljs-string">&apos;404.html&apos;</span>
  <span class="hljs-keyword">switch</span> ( url ) {
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/&apos;</span>:
      view = <span class="hljs-string">&apos;index.html&apos;</span>
      <span class="hljs-keyword">break</span>
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/index&apos;</span>:
      view = <span class="hljs-string">&apos;index.html&apos;</span>
      <span class="hljs-keyword">break</span>
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/todo&apos;</span>:
      view = <span class="hljs-string">&apos;todo.html&apos;</span>
      <span class="hljs-keyword">break</span>
    <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;/404&apos;</span>:
      view = <span class="hljs-string">&apos;404.html&apos;</span>
      <span class="hljs-keyword">break</span>
    <span class="hljs-keyword">default</span>:
      <span class="hljs-keyword">break</span>
  }
  <span class="hljs-keyword">let</span> html = <span class="hljs-keyword">await</span> render( view )
  <span class="hljs-keyword">return</span> html
}

app.use( <span class="hljs-keyword">async</span> ( ctx ) =&gt; {
  <span class="hljs-keyword">let</span> url = ctx.request.url
  <span class="hljs-keyword">let</span> html = <span class="hljs-keyword">await</span> route( url )
  ctx.body = html
})

app.listen(<span class="hljs-number">3000</span>)
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;[demo] route-simple is starting at port 3000&apos;</span>)
</code></pre>
<h5 id="23-&#x8FD0;&#x884C;demo"><a name="23-&#x8FD0;&#x884C;demo" class="anchor-navigation-ex-anchor" href="#23-&#x8FD0;&#x884C;demo"><i class="fa fa-link" aria-hidden="true"></i></a><a name="23-&#x8FD0;&#x884C;demo" class="plugin-anchor" href="#23-&#x8FD0;&#x884C;demo"><i class="fa fa-link" aria-hidden="true"></i></a>2.3 &#x8FD0;&#x884C;demo</h5>
<p><strong>&#x6267;&#x884C;&#x8FD0;&#x884C;&#x811A;&#x672C;</strong></p>
<pre><code>node -harmony index.js
</code></pre><h3 id="&#x4E8C;&#x3001;koa-router&#x4E2D;&#x95F4;&#x4EF6;"><a name="&#x4E8C;&#x3001;koa-router&#x4E2D;&#x95F4;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#&#x4E8C;&#x3001;koa-router&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E8C;&#x3001;koa-router&#x4E2D;&#x95F4;&#x4EF6;" class="plugin-anchor" href="#&#x4E8C;&#x3001;koa-router&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C;&#x3001;koa-router&#x4E2D;&#x95F4;&#x4EF6;</h3>
<p>&#x5982;&#x679C;&#x4F9D;&#x9760;ctx.request.url&#x53BB;&#x624B;&#x52A8;&#x5904;&#x7406;&#x8DEF;&#x7531;&#xFF0C;&#x5C06;&#x4F1A;&#x5199;&#x5F88;&#x591A;&#x5904;&#x7406;&#x4EE3;&#x7801;&#xFF0C;&#x8FD9;&#x65F6;&#x5019;&#x5C31;&#x9700;&#x8981;&#x5BF9;&#x5E94;&#x7684;&#x8DEF;&#x7531;&#x7684;&#x4E2D;&#x95F4;&#x4EF6;&#x5BF9;&#x8DEF;&#x7531;&#x8FDB;&#x884C;&#x63A7;&#x5236;&#xFF0C;&#x8FD9;&#x91CC;&#x4ECB;&#x7ECD;&#x4E00;&#x4E2A;&#x6BD4;&#x8F83;&#x597D;&#x7528;&#x7684;&#x8DEF;&#x7531;&#x4E2D;&#x95F4;&#x4EF6;koa-router</p>
<h4 id="1&#x3001;&#x5B89;&#x88C5;koa-router&#x4E2D;&#x95F4;&#x4EF6;"><a name="1&#x3001;&#x5B89;&#x88C5;koa-router&#x4E2D;&#x95F4;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x5B89;&#x88C5;koa-router&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x5B89;&#x88C5;koa-router&#x4E2D;&#x95F4;&#x4EF6;" class="plugin-anchor" href="#1&#x3001;&#x5B89;&#x88C5;koa-router&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x5B89;&#x88C5;koa-router&#x4E2D;&#x95F4;&#x4EF6;</h4>
<pre><code># koa2 &#x5BF9;&#x5E94;&#x7684;&#x7248;&#x672C;&#x662F; 7.x
npm install --save koa-router@7
</code></pre><h4 id="2&#x3001;&#x5FEB;&#x901F;&#x4F7F;&#x7528;koa-router"><a name="2&#x3001;&#x5FEB;&#x901F;&#x4F7F;&#x7528;koa-router" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x5FEB;&#x901F;&#x4F7F;&#x7528;koa-router"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x5FEB;&#x901F;&#x4F7F;&#x7528;koa-router" class="plugin-anchor" href="#2&#x3001;&#x5FEB;&#x901F;&#x4F7F;&#x7528;koa-router"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x5FEB;&#x901F;&#x4F7F;&#x7528;koa-router</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>)
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>)
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()

<span class="hljs-keyword">const</span> Router = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa-router&apos;</span>)

<span class="hljs-keyword">let</span> home = <span class="hljs-keyword">new</span> Router()

<span class="hljs-comment">// &#x5B50;&#x8DEF;&#x7531;1</span>
home.get(<span class="hljs-string">&apos;/&apos;</span>, <span class="hljs-keyword">async</span> ( ctx )=&gt;{
  <span class="hljs-keyword">let</span> html = <span class="hljs-string">`
    &lt;ul&gt;
      &lt;li&gt;&lt;a href=&quot;/page/helloworld&quot;&gt;/page/helloworld&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;&lt;a href=&quot;/page/404&quot;&gt;/page/404&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  `</span>
  ctx.body = html
})

<span class="hljs-comment">// &#x5B50;&#x8DEF;&#x7531;2</span>
<span class="hljs-keyword">let</span> page = <span class="hljs-keyword">new</span> Router()
page.get(<span class="hljs-string">&apos;/404&apos;</span>, <span class="hljs-keyword">async</span> ( ctx )=&gt;{
  ctx.body = <span class="hljs-string">&apos;404 page!&apos;</span>
}).get(<span class="hljs-string">&apos;/helloworld&apos;</span>, <span class="hljs-keyword">async</span> ( ctx )=&gt;{
  ctx.body = <span class="hljs-string">&apos;helloworld page!&apos;</span>
})

<span class="hljs-comment">// &#x88C5;&#x8F7D;&#x6240;&#x6709;&#x5B50;&#x8DEF;&#x7531;</span>
<span class="hljs-keyword">let</span> router = <span class="hljs-keyword">new</span> Router()
router.use(<span class="hljs-string">&apos;/&apos;</span>, home.routes(), home.allowedMethods())
router.use(<span class="hljs-string">&apos;/page&apos;</span>, page.routes(), page.allowedMethods())

<span class="hljs-comment">// &#x52A0;&#x8F7D;&#x8DEF;&#x7531;&#x4E2D;&#x95F4;&#x4EF6;</span>
app.use(router.routes()).use(router.allowedMethods())

app.listen(<span class="hljs-number">3000</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;[demo] route-use-middleware is starting at port 3000&apos;</span>)
})
</code></pre>
<h1 id="&#x2172;&#x3001;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;"><a name="&#x2172;&#x3001;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;" class="anchor-navigation-ex-anchor" href="#&#x2172;&#x3001;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x2172;&#x3001;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;" class="plugin-anchor" href="#&#x2172;&#x3001;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x2162;&#x3001;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;</h1>
<h3 id="&#x4E00;&#x3001;get&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;"><a name="&#x4E00;&#x3001;get&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;" class="anchor-navigation-ex-anchor" href="#&#x4E00;&#x3001;get&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E00;&#x3001;get&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;" class="plugin-anchor" href="#&#x4E00;&#x3001;get&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E00;&#x3001;GET&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x83B7;&#x53D6;</h3>
<h4 id="1&#x3001;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;"><a name="1&#x3001;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;" class="plugin-anchor" href="#1&#x3001;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x4F7F;&#x7528;&#x65B9;&#x6CD5;</h4>
<p>&#x5728;koa&#x4E2D;&#xFF0C;&#x83B7;&#x53D6;GET&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x6E90;&#x5934;&#x662F;koa&#x4E2D;request&#x5BF9;&#x8C61;&#x4E2D;&#x7684;query&#x65B9;&#x6CD5;&#x6216;querystring&#x65B9;&#x6CD5;&#xFF0C;query&#x8FD4;&#x56DE;&#x662F;&#x683C;&#x5F0F;&#x5316;&#x597D;&#x7684;&#x53C2;&#x6570;&#x5BF9;&#x8C61;&#xFF0C;querystring&#x8FD4;&#x56DE;&#x7684;&#x662F;&#x8BF7;&#x6C42;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x7531;&#x4E8E;ctx&#x5BF9;request&#x7684;API&#x6709;&#x76F4;&#x63A5;&#x5F15;&#x7528;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x6240;&#x4EE5;&#x83B7;&#x53D6;GET&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x6709;&#x4E24;&#x4E2A;&#x9014;&#x5F84;&#x3002;</p>
<ul>
<li><p>&#x662F;&#x4ECE;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x76F4;&#x63A5;&#x83B7;&#x53D6;
&#x8BF7;&#x6C42;&#x5BF9;&#x8C61;ctx.query&#xFF0C;&#x8FD4;&#x56DE;&#x5982; { a:1, b:2 }
&#x8BF7;&#x6C42;&#x5B57;&#x7B26;&#x4E32; ctx.querystring&#xFF0C;&#x8FD4;&#x56DE;&#x5982; a=1&amp;b=2</p>
</li>
<li><p>&#x662F;&#x4ECE;&#x4E0A;&#x4E0B;&#x6587;&#x7684;request&#x5BF9;&#x8C61;&#x4E2D;&#x83B7;&#x53D6;
&#x8BF7;&#x6C42;&#x5BF9;&#x8C61;ctx.request.query&#xFF0C;&#x8FD4;&#x56DE;&#x5982; { a:1, b:2 }
&#x8BF7;&#x6C42;&#x5B57;&#x7B26;&#x4E32; ctx.request.querystring&#xFF0C;&#x8FD4;&#x56DE;&#x5982; a=1&amp;b=2</p>
</li>
</ul>
<h4 id="2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;"><a name="2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;" class="plugin-anchor" href="#2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;</h4>
<h5 id="21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;"><a name="21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;" class="anchor-navigation-ex-anchor" href="#21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;" class="plugin-anchor" href="#21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1 &#x4F8B;&#x5B50;&#x4EE3;&#x7801;</h5>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>)
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()

app.use( <span class="hljs-keyword">async</span> ( ctx ) =&gt; {
  <span class="hljs-keyword">let</span> url = ctx.url
  <span class="hljs-comment">// &#x4ECE;&#x4E0A;&#x4E0B;&#x6587;&#x7684;request&#x5BF9;&#x8C61;&#x4E2D;&#x83B7;&#x53D6;</span>
  <span class="hljs-keyword">let</span> request = ctx.request
  <span class="hljs-keyword">let</span> req_query = request.query
  <span class="hljs-keyword">let</span> req_querystring = request.querystring

  <span class="hljs-comment">// &#x4ECE;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x76F4;&#x63A5;&#x83B7;&#x53D6;</span>
  <span class="hljs-keyword">let</span> ctx_query = ctx.query
  <span class="hljs-keyword">let</span> ctx_querystring = ctx.querystring

  ctx.body = {
    url,
    req_query,
    req_querystring,
    ctx_query,
    ctx_querystring
  }
})

app.listen(<span class="hljs-number">3000</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;[demo] request get is starting at port 3000&apos;</span>)
})
</code></pre>
<h5 id="22-&#x6267;&#x884C;&#x7A0B;&#x5E8F;"><a name="22-&#x6267;&#x884C;&#x7A0B;&#x5E8F;" class="anchor-navigation-ex-anchor" href="#22-&#x6267;&#x884C;&#x7A0B;&#x5E8F;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="22-&#x6267;&#x884C;&#x7A0B;&#x5E8F;" class="plugin-anchor" href="#22-&#x6267;&#x884C;&#x7A0B;&#x5E8F;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2 &#x6267;&#x884C;&#x7A0B;&#x5E8F;</h5>
<pre><code class="lang-s">node get.js
</code></pre>
<h3 id="&#x4E8C;&#x3001;post&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x83B7;&#x53D6;"><a name="&#x4E8C;&#x3001;post&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x83B7;&#x53D6;" class="anchor-navigation-ex-anchor" href="#&#x4E8C;&#x3001;post&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E8C;&#x3001;post&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x83B7;&#x53D6;" class="plugin-anchor" href="#&#x4E8C;&#x3001;post&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x83B7;&#x53D6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E8C;&#x3001;POST&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x83B7;&#x53D6;</h3>
<h4 id="1&#x3001;&#x539F;&#x7406;"><a name="1&#x3001;&#x539F;&#x7406;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x539F;&#x7406;" class="plugin-anchor" href="#1&#x3001;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x539F;&#x7406;</h4>
<p>&#x5BF9;&#x4E8E;POST&#x8BF7;&#x6C42;&#x7684;&#x5904;&#x7406;&#xFF0C;koa2&#x6CA1;&#x6709;&#x5C01;&#x88C5;&#x83B7;&#x53D6;&#x53C2;&#x6570;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x89E3;&#x6790;&#x4E0A;&#x4E0B;&#x6587;context&#x4E2D;&#x7684;&#x539F;&#x751F;node.js&#x8BF7;&#x6C42;&#x5BF9;&#x8C61;req&#xFF0C;&#x5C06;POST&#x8868;&#x5355;&#x6570;&#x636E;&#x89E3;&#x6790;&#x6210;query string&#xFF08;&#x4F8B;&#x5982;&#xFF1A;a=1&amp;b=2&amp;c=3&#xFF09;&#xFF0C;&#x518D;&#x5C06;query string &#x89E3;&#x6790;&#x6210;JSON&#x683C;&#x5F0F;&#xFF08;&#x4F8B;&#x5982;&#xFF1A;{&quot;a&quot;:&quot;1&quot;, &quot;b&quot;:&quot;2&quot;, &quot;c&quot;:&quot;3&quot;}&#xFF09;</p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;ctx.request&#x662F;context&#x7ECF;&#x8FC7;&#x5C01;&#x88C5;&#x7684;&#x8BF7;&#x6C42;&#x5BF9;&#x8C61;&#xFF0C;ctx.req&#x662F;context&#x63D0;&#x4F9B;&#x7684;node.js&#x539F;&#x751F;HTTP&#x8BF7;&#x6C42;&#x5BF9;&#x8C61;&#xFF0C;&#x540C;&#x7406;ctx.response&#x662F;context&#x7ECF;&#x8FC7;&#x5C01;&#x88C5;&#x7684;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#xFF0C;ctx.res&#x662F;context&#x63D0;&#x4F9B;&#x7684;node.js&#x539F;&#x751F;HTTP&#x8BF7;&#x6C42;&#x5BF9;&#x8C61;&#x3002;</p>
</blockquote>
<h5 id="&#x89E3;&#x6790;&#x51FA;post&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x7684;&#x8868;&#x5355;&#x6570;&#x636E;"><a name="&#x89E3;&#x6790;&#x51FA;post&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x7684;&#x8868;&#x5355;&#x6570;&#x636E;" class="anchor-navigation-ex-anchor" href="#&#x89E3;&#x6790;&#x51FA;post&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x7684;&#x8868;&#x5355;&#x6570;&#x636E;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x89E3;&#x6790;&#x51FA;post&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x7684;&#x8868;&#x5355;&#x6570;&#x636E;" class="plugin-anchor" href="#&#x89E3;&#x6790;&#x51FA;post&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x7684;&#x8868;&#x5355;&#x6570;&#x636E;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x89E3;&#x6790;&#x51FA;POST&#x8BF7;&#x6C42;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x7684;&#x8868;&#x5355;&#x6570;&#x636E;</h5>
<pre><code class="lang-js"><span class="hljs-comment">// &#x89E3;&#x6790;&#x4E0A;&#x4E0B;&#x6587;&#x91CC;node&#x539F;&#x751F;&#x8BF7;&#x6C42;&#x7684;POST&#x53C2;&#x6570;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">parsePostData</span>(<span class="hljs-params"> ctx </span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">let</span> postdata = <span class="hljs-string">&quot;&quot;</span>;
      ctx.req.addListener(<span class="hljs-string">&apos;data&apos;</span>, (data) =&gt; {
        postdata += data
      })
      ctx.req.addListener(<span class="hljs-string">&quot;end&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
        <span class="hljs-keyword">let</span> parseData = parseQueryStr( postdata )
        resolve( parseData )
      })
    } <span class="hljs-keyword">catch</span> ( err ) {
      reject(err)
    }
  })
}

<span class="hljs-comment">// &#x5C06;POST&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x5B57;&#x7B26;&#x4E32;&#x89E3;&#x6790;&#x6210;JSON</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">parseQueryStr</span>(<span class="hljs-params"> queryStr </span>) </span>{
  <span class="hljs-keyword">let</span> queryData = {}
  <span class="hljs-keyword">let</span> queryStrList = queryStr.split(<span class="hljs-string">&apos;&amp;&apos;</span>)
  <span class="hljs-built_in">console</span>.log( queryStrList )
  <span class="hljs-keyword">for</span> (  <span class="hljs-keyword">let</span> [ index, queryStr ] <span class="hljs-keyword">of</span> queryStrList.entries()  ) {
    <span class="hljs-keyword">let</span> itemList = queryStr.split(<span class="hljs-string">&apos;=&apos;</span>)
    queryData[ itemList[<span class="hljs-number">0</span>] ] = <span class="hljs-built_in">decodeURIComponent</span>(itemList[<span class="hljs-number">1</span>])
  }
  <span class="hljs-keyword">return</span> queryData
}
</code></pre>
<h4 id="2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;_1"><a name="2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;_1" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;_1"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;" class="plugin-anchor" href="#2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;</h4>
<h5 id="21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;_1"><a name="21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;_1" class="anchor-navigation-ex-anchor" href="#21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;_1"><i class="fa fa-link" aria-hidden="true"></i></a><a name="21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;" class="plugin-anchor" href="#21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1 &#x4F8B;&#x5B50;&#x4EE3;&#x7801;</h5>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>)
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()

app.use( <span class="hljs-keyword">async</span> ( ctx ) =&gt; {

  <span class="hljs-keyword">if</span> ( ctx.url === <span class="hljs-string">&apos;/&apos;</span> &amp;&amp; ctx.method === <span class="hljs-string">&apos;GET&apos;</span> ) {
    <span class="hljs-comment">// &#x5F53;GET&#x8BF7;&#x6C42;&#x65F6;&#x5019;&#x8FD4;&#x56DE;&#x8868;&#x5355;&#x9875;&#x9762;</span>
    <span class="hljs-keyword">let</span> html = <span class="hljs-string">`
      &lt;h1&gt;koa2 request post demo&lt;/h1&gt;
      &lt;form method=&quot;POST&quot; action=&quot;/&quot;&gt;
        &lt;p&gt;userName&lt;/p&gt;
        &lt;input name=&quot;userName&quot; /&gt;&lt;br/&gt;
        &lt;p&gt;nickName&lt;/p&gt;
        &lt;input name=&quot;nickName&quot; /&gt;&lt;br/&gt;
        &lt;p&gt;email&lt;/p&gt;
        &lt;input name=&quot;email&quot; /&gt;&lt;br/&gt;
        &lt;button type=&quot;submit&quot;&gt;submit&lt;/button&gt;
      &lt;/form&gt;
    `</span>
    ctx.body = html
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ( ctx.url === <span class="hljs-string">&apos;/&apos;</span> &amp;&amp; ctx.method === <span class="hljs-string">&apos;POST&apos;</span> ) {
    <span class="hljs-comment">// &#x5F53;POST&#x8BF7;&#x6C42;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x89E3;&#x6790;POST&#x8868;&#x5355;&#x91CC;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x663E;&#x793A;&#x51FA;&#x6765;</span>
    <span class="hljs-keyword">let</span> postData = <span class="hljs-keyword">await</span> parsePostData( ctx )
    ctx.body = postData
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// &#x5176;&#x4ED6;&#x8BF7;&#x6C42;&#x663E;&#x793A;404</span>
    ctx.body = <span class="hljs-string">&apos;&lt;h1&gt;404&#xFF01;&#xFF01;&#xFF01; o(&#x256F;&#x25A1;&#x2570;)o&lt;/h1&gt;&apos;</span>
  }
})

<span class="hljs-comment">// &#x89E3;&#x6790;&#x4E0A;&#x4E0B;&#x6587;&#x91CC;node&#x539F;&#x751F;&#x8BF7;&#x6C42;&#x7684;POST&#x53C2;&#x6570;</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">parsePostData</span>(<span class="hljs-params"> ctx </span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>((resolve, reject) =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">let</span> postdata = <span class="hljs-string">&quot;&quot;</span>;
      ctx.req.addListener(<span class="hljs-string">&apos;data&apos;</span>, (data) =&gt; {
        postdata += data
      })
      ctx.req.addListener(<span class="hljs-string">&quot;end&quot;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
        <span class="hljs-keyword">let</span> parseData = parseQueryStr( postdata )
        resolve( parseData )
      })
    } <span class="hljs-keyword">catch</span> ( err ) {
      reject(err)
    }
  })
}

<span class="hljs-comment">// &#x5C06;POST&#x8BF7;&#x6C42;&#x53C2;&#x6570;&#x5B57;&#x7B26;&#x4E32;&#x89E3;&#x6790;&#x6210;JSON</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">parseQueryStr</span>(<span class="hljs-params"> queryStr </span>) </span>{
  <span class="hljs-keyword">let</span> queryData = {}
  <span class="hljs-keyword">let</span> queryStrList = queryStr.split(<span class="hljs-string">&apos;&amp;&apos;</span>)
  <span class="hljs-built_in">console</span>.log( queryStrList )
  <span class="hljs-keyword">for</span> (  <span class="hljs-keyword">let</span> [ index, queryStr ] <span class="hljs-keyword">of</span> queryStrList.entries()  ) {
    <span class="hljs-keyword">let</span> itemList = queryStr.split(<span class="hljs-string">&apos;=&apos;</span>)
    queryData[ itemList[<span class="hljs-number">0</span>] ] = <span class="hljs-built_in">decodeURIComponent</span>(itemList[<span class="hljs-number">1</span>])
  }
  <span class="hljs-keyword">return</span> queryData
}

app.listen(<span class="hljs-number">3000</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;[demo] request post is starting at port 3000&apos;</span>)
})
</code></pre>
<h5 id="22-&#x542F;&#x52A8;&#x4F8B;&#x5B50;"><a name="22-&#x542F;&#x52A8;&#x4F8B;&#x5B50;" class="anchor-navigation-ex-anchor" href="#22-&#x542F;&#x52A8;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="22-&#x542F;&#x52A8;&#x4F8B;&#x5B50;" class="plugin-anchor" href="#22-&#x542F;&#x52A8;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2 &#x542F;&#x52A8;&#x4F8B;&#x5B50;</h5>
<pre><code class="lang-s">node post.js
</code></pre>
<h3 id="&#x4E09;&#x3001;koa-bodyparser&#x4E2D;&#x95F4;&#x4EF6;"><a name="&#x4E09;&#x3001;koa-bodyparser&#x4E2D;&#x95F4;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#&#x4E09;&#x3001;koa-bodyparser&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x4E09;&#x3001;koa-bodyparser&#x4E2D;&#x95F4;&#x4EF6;" class="plugin-anchor" href="#&#x4E09;&#x3001;koa-bodyparser&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x4E09;&#x3001;koa-bodyparser&#x4E2D;&#x95F4;&#x4EF6;</h3>
<h4 id="1&#x3001;&#x539F;&#x7406;_1"><a name="1&#x3001;&#x539F;&#x7406;_1" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x539F;&#x7406;_1"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x539F;&#x7406;" class="plugin-anchor" href="#1&#x3001;&#x539F;&#x7406;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x539F;&#x7406;</h4>
<p>&#x5BF9;&#x4E8E;POST&#x8BF7;&#x6C42;&#x7684;&#x5904;&#x7406;&#xFF0C;koa-bodyparser&#x4E2D;&#x95F4;&#x4EF6;&#x53EF;&#x4EE5;&#x628A;koa2&#x4E0A;&#x4E0B;&#x6587;&#x7684;formData&#x6570;&#x636E;&#x89E3;&#x6790;&#x5230;ctx.request.body&#x4E2D;</p>
<h5 id="&#x5B89;&#x88C5;koa2&#x7248;&#x672C;&#x7684;koa-bodyparser3&#x4E2D;&#x95F4;&#x4EF6;"><a name="&#x5B89;&#x88C5;koa2&#x7248;&#x672C;&#x7684;koa-bodyparser3&#x4E2D;&#x95F4;&#x4EF6;" class="anchor-navigation-ex-anchor" href="#&#x5B89;&#x88C5;koa2&#x7248;&#x672C;&#x7684;koa-bodyparser3&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x5B89;&#x88C5;koa2&#x7248;&#x672C;&#x7684;koa-bodyparser3&#x4E2D;&#x95F4;&#x4EF6;" class="plugin-anchor" href="#&#x5B89;&#x88C5;koa2&#x7248;&#x672C;&#x7684;koa-bodyparser3&#x4E2D;&#x95F4;&#x4EF6;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x5B89;&#x88C5;koa2&#x7248;&#x672C;&#x7684;koa-bodyparser@3&#x4E2D;&#x95F4;&#x4EF6;</h5>
<pre><code class="lang-s">npm install --save koa-bodyparser@3
</code></pre>
<h4 id="2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;_2"><a name="2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;_2" class="anchor-navigation-ex-anchor" href="#2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;_2"><i class="fa fa-link" aria-hidden="true"></i></a><a name="2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;" class="plugin-anchor" href="#2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a>2&#x3001;&#x4E3E;&#x4E2A;&#x4F8B;&#x5B50;</h4>
<h5 id="21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;_2"><a name="21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;_2" class="anchor-navigation-ex-anchor" href="#21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;_2"><i class="fa fa-link" aria-hidden="true"></i></a><a name="21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;" class="plugin-anchor" href="#21-&#x4F8B;&#x5B50;&#x4EE3;&#x7801;"><i class="fa fa-link" aria-hidden="true"></i></a>2.1 &#x4F8B;&#x5B50;&#x4EE3;&#x7801;</h5>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>)
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()
<span class="hljs-keyword">const</span> bodyParser = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa-bodyparser&apos;</span>)

<span class="hljs-comment">// &#x4F7F;&#x7528;ctx.body&#x89E3;&#x6790;&#x4E2D;&#x95F4;&#x4EF6;</span>
app.use(bodyParser())

app.use( <span class="hljs-keyword">async</span> ( ctx ) =&gt; {

  <span class="hljs-keyword">if</span> ( ctx.url === <span class="hljs-string">&apos;/&apos;</span> &amp;&amp; ctx.method === <span class="hljs-string">&apos;GET&apos;</span> ) {
    <span class="hljs-comment">// &#x5F53;GET&#x8BF7;&#x6C42;&#x65F6;&#x5019;&#x8FD4;&#x56DE;&#x8868;&#x5355;&#x9875;&#x9762;</span>
    <span class="hljs-keyword">let</span> html = <span class="hljs-string">`
      &lt;h1&gt;koa2 request post demo&lt;/h1&gt;
      &lt;form method=&quot;POST&quot; action=&quot;/&quot;&gt;
        &lt;p&gt;userName&lt;/p&gt;
        &lt;input name=&quot;userName&quot; /&gt;&lt;br/&gt;
        &lt;p&gt;nickName&lt;/p&gt;
        &lt;input name=&quot;nickName&quot; /&gt;&lt;br/&gt;
        &lt;p&gt;email&lt;/p&gt;
        &lt;input name=&quot;email&quot; /&gt;&lt;br/&gt;
        &lt;button type=&quot;submit&quot;&gt;submit&lt;/button&gt;
      &lt;/form&gt;
    `</span>
    ctx.body = html
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> ( ctx.url === <span class="hljs-string">&apos;/&apos;</span> &amp;&amp; ctx.method === <span class="hljs-string">&apos;POST&apos;</span> ) {
    <span class="hljs-comment">// &#x5F53;POST&#x8BF7;&#x6C42;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x4E2D;&#x95F4;&#x4EF6;koa-bodyparser&#x89E3;&#x6790;POST&#x8868;&#x5355;&#x91CC;&#x7684;&#x6570;&#x636E;&#xFF0C;&#x5E76;&#x663E;&#x793A;&#x51FA;&#x6765;</span>
    <span class="hljs-keyword">let</span> postData = ctx.request.body
    ctx.body = postData
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-comment">// &#x5176;&#x4ED6;&#x8BF7;&#x6C42;&#x663E;&#x793A;404</span>
    ctx.body = <span class="hljs-string">&apos;&lt;h1&gt;404&#xFF01;&#xFF01;&#xFF01; o(&#x256F;&#x25A1;&#x2570;)o&lt;/h1&gt;&apos;</span>
  }
})

app.listen(<span class="hljs-number">3000</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;[demo] request post is starting at port 3000&apos;</span>)
})
</code></pre>
<h5 id="22-&#x542F;&#x52A8;&#x4F8B;&#x5B50;_1"><a name="22-&#x542F;&#x52A8;&#x4F8B;&#x5B50;_1" class="anchor-navigation-ex-anchor" href="#22-&#x542F;&#x52A8;&#x4F8B;&#x5B50;_1"><i class="fa fa-link" aria-hidden="true"></i></a><a name="22-&#x542F;&#x52A8;&#x4F8B;&#x5B50;" class="plugin-anchor" href="#22-&#x542F;&#x52A8;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a>2.2 &#x542F;&#x52A8;&#x4F8B;&#x5B50;</h5>
<pre><code class="lang-s">node post-middleware.js
</code></pre>
<h1 id="&#x2173;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x52A0;&#x8F7D;"><a name="&#x2173;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x52A0;&#x8F7D;" class="anchor-navigation-ex-anchor" href="#&#x2173;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x52A0;&#x8F7D;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x2173;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x52A0;&#x8F7D;" class="plugin-anchor" href="#&#x2173;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x52A0;&#x8F7D;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x2163;&#x3001;&#x9759;&#x6001;&#x8D44;&#x6E90;&#x52A0;&#x8F7D;</h1>
<h3 id="koa-static&#x4E2D;&#x95F4;&#x4EF6;&#x4F7F;&#x7528;"><a name="koa-static&#x4E2D;&#x95F4;&#x4EF6;&#x4F7F;&#x7528;" class="anchor-navigation-ex-anchor" href="#koa-static&#x4E2D;&#x95F4;&#x4EF6;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="koa-static&#x4E2D;&#x95F4;&#x4EF6;&#x4F7F;&#x7528;" class="plugin-anchor" href="#koa-static&#x4E2D;&#x95F4;&#x4EF6;&#x4F7F;&#x7528;"><i class="fa fa-link" aria-hidden="true"></i></a>koa-static&#x4E2D;&#x95F4;&#x4EF6;&#x4F7F;&#x7528;</h3>
<h4 id="1&#x3001;&#x4F7F;&#x7528;&#x4F8B;&#x5B50;"><a name="1&#x3001;&#x4F7F;&#x7528;&#x4F8B;&#x5B50;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x4F7F;&#x7528;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x4F7F;&#x7528;&#x4F8B;&#x5B50;" class="plugin-anchor" href="#1&#x3001;&#x4F7F;&#x7528;&#x4F8B;&#x5B50;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x4F7F;&#x7528;&#x4F8B;&#x5B50;</h4>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>)
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>)
<span class="hljs-keyword">const</span> <span class="hljs-keyword">static</span> = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa-static&apos;</span>)

<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()

<span class="hljs-comment">// &#x9759;&#x6001;&#x8D44;&#x6E90;&#x76EE;&#x5F55;&#x5BF9;&#x4E8E;&#x76F8;&#x5BF9;&#x5165;&#x53E3;&#x6587;&#x4EF6;index.js&#x7684;&#x8DEF;&#x5F84;</span>
<span class="hljs-keyword">const</span> staticPath = <span class="hljs-string">&apos;./static&apos;</span>

app.use(<span class="hljs-keyword">static</span>(
  path.join( __dirname,  staticPath)
))


app.use( <span class="hljs-keyword">async</span> ( ctx ) =&gt; {
  ctx.body = <span class="hljs-string">&apos;hello world&apos;</span>
})

app.listen(<span class="hljs-number">3000</span>, () =&gt; {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;[demo] static-use-middleware is starting at port 3000&apos;</span>)
})
</code></pre>
<h1 id="&#x2174;&#x3001;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><a name="&#x2174;&#x3001;&#x6A21;&#x677F;&#x5F15;&#x64CE;" class="anchor-navigation-ex-anchor" href="#&#x2174;&#x3001;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="&#x2174;&#x3001;&#x6A21;&#x677F;&#x5F15;&#x64CE;" class="plugin-anchor" href="#&#x2174;&#x3001;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><i class="fa fa-link" aria-hidden="true"></i></a>&#x2164;&#x3001;&#x6A21;&#x677F;&#x5F15;&#x64CE;</h1>
<h3 id="koa2&#x52A0;&#x8F7D;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><a name="koa2&#x52A0;&#x8F7D;&#x6A21;&#x677F;&#x5F15;&#x64CE;" class="anchor-navigation-ex-anchor" href="#koa2&#x52A0;&#x8F7D;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="koa2&#x52A0;&#x8F7D;&#x6A21;&#x677F;&#x5F15;&#x64CE;" class="plugin-anchor" href="#koa2&#x52A0;&#x8F7D;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><i class="fa fa-link" aria-hidden="true"></i></a>koa2&#x52A0;&#x8F7D;&#x6A21;&#x677F;&#x5F15;&#x64CE;</h3>
<h4 id="1&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;"><a name="1&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;" class="anchor-navigation-ex-anchor" href="#1&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="1&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;" class="plugin-anchor" href="#1&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;"><i class="fa fa-link" aria-hidden="true"></i></a>1&#x3001;&#x5FEB;&#x901F;&#x5F00;&#x59CB;</h4>
<h5 id="11-&#x5B89;&#x88C5;&#x6A21;&#x5757;"><a name="11-&#x5B89;&#x88C5;&#x6A21;&#x5757;" class="anchor-navigation-ex-anchor" href="#11-&#x5B89;&#x88C5;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="11-&#x5B89;&#x88C5;&#x6A21;&#x5757;" class="plugin-anchor" href="#11-&#x5B89;&#x88C5;&#x6A21;&#x5757;"><i class="fa fa-link" aria-hidden="true"></i></a>1.1 &#x5B89;&#x88C5;&#x6A21;&#x5757;</h5>
<pre><code class="lang-s"># &#x5B89;&#x88C5;koa&#x6A21;&#x677F;&#x4F7F;&#x7528;&#x4E2D;&#x95F4;&#x4EF6;
npm install --save koa-views

# &#x5B89;&#x88C5;ejs&#x6A21;&#x677F;&#x5F15;&#x64CE;
npm install --save ejs
</code></pre>
<h5 id="12-&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><a name="12-&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;" class="anchor-navigation-ex-anchor" href="#12-&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><i class="fa fa-link" aria-hidden="true"></i></a><a name="12-&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;" class="plugin-anchor" href="#12-&#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;"><i class="fa fa-link" aria-hidden="true"></i></a>1.2 &#x4F7F;&#x7528;&#x6A21;&#x677F;&#x5F15;&#x64CE;</h5>
<p><strong>&#x6587;&#x4EF6;&#x76EE;&#x5F55;</strong></p>
<pre><code>&#x251C;&#x2500;&#x2500; package.json
&#x251C;&#x2500;&#x2500; index.js
&#x2514;&#x2500;&#x2500; view
    &#x2514;&#x2500;&#x2500; index.ejs
</code></pre><p><strong>./index.js&#x6587;&#x4EF6;</strong></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> Koa = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa&apos;</span>)
<span class="hljs-keyword">const</span> views = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;koa-views&apos;</span>)
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>)
<span class="hljs-keyword">const</span> app = <span class="hljs-keyword">new</span> Koa()

<span class="hljs-comment">// &#x52A0;&#x8F7D;&#x6A21;&#x677F;&#x5F15;&#x64CE;</span>
app.use(views(path.join(__dirname, <span class="hljs-string">&apos;./view&apos;</span>), {
  extension: <span class="hljs-string">&apos;ejs&apos;</span>
}))

app.use( <span class="hljs-keyword">async</span> ( ctx ) =&gt; {
  <span class="hljs-keyword">let</span> title = <span class="hljs-string">&apos;hello koa2&apos;</span>
  <span class="hljs-keyword">await</span> ctx.render(<span class="hljs-string">&apos;index&apos;</span>, {
    title,
  })
})

app.listen(<span class="hljs-number">3000</span>)
</code></pre>
<p><strong>./view/index.ejs &#x6A21;&#x677F;</strong></p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE html&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">%=</span> <span class="hljs-attr">title</span> %&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">%=</span> <span class="hljs-attr">title</span> %&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>EJS Welcome to <span class="hljs-tag">&lt;<span class="hljs-name">%=</span> <span class="hljs-attr">title</span> %&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="03-Express.html" class="navigation navigation-prev " aria-label="Previous page: 03-Express">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="05-MongoDB.html" class="navigation navigation-next " aria-label="Next page: 05-MongoDB">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"04-Koa2","level":"1.2.4","depth":2,"next":{"title":"05-MongoDB","level":"1.2.5","depth":2,"path":"basics/05-MongoDB.md","ref":"./basics/05-MongoDB.md","articles":[]},"previous":{"title":"03-Express","level":"1.2.3","depth":2,"path":"basics/03-Express.md","ref":"./basics/03-Express.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-sharing","splitter","expandable-chapters-small","anchors","github","github-buttons","sharing-plus","anchor-navigation-ex","favicon","insert-logo","livereload"],"styles":{"website":"./styles/website.css"},"pluginsConfig":{"github":{"url":"https://gitee.com/lurongtao/felixbooks-gp19-Node.js"},"livereload":{},"splitter":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"anchor-navigation-ex":{"associatedWithSummary":true,"float":{"floatIcon":"fa fa-navicon","level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"mode":"float","multipleH1":true,"pageTop":{"level1Icon":"","level2Icon":"","level3Icon":"","showLevelIcon":false},"printLog":false,"showGoTop":true,"showLevel":false},"favicon":{"shortcut":"./source/images/favicon.jpg","bookmark":"./source/images/favicon.jpg","appleTouch":"./source/images/apple-touch-icon.jpg","appleTouchMore":{"120x120":"./source/images/apple-touch-icon.jpg","180x180":"./source/images/apple-touch-icon.jpg"}},"github-buttons":{"buttons":[{"user":"lurongtao","repo":"felixbooks-kbone","type":"star","size":"small","count":true}]},"expandable-chapters-small":{},"sharing":{"qq":false,"all":["google","facebook","weibo","twitter","qq","qzone","linkedin","pocket"],"douban":false,"facebook":false,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":false,"messenger":false,"line":false,"vk":false,"pocket":false,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false},"anchors":{},"insert-logo":{"url":"/felixbooks-gp19-node.js/images/node.js.jpg","style":"background: skyblue; width: 100%; height: 100%;"}},"theme":"default","author":"古艺散人","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"千锋大前端小册-GP19-Node.js","language":"zh-hans","links":{},"gitbook":"3.2.3","description":"Node.js复习"},"file":{"path":"basics/04-Koa2.md","mtime":"2020-06-25T01:23:59.580Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-07-15T09:28:56.165Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-expandable-chapters-small/expandable-chapters-small.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-github-buttons/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

